#include "WindowManager.h"

#include "GameService.h"

using namespace std;

//-------------------------------------------------------------------------------------------
WindowManager::WindowManager()
{
	sf::ContextSettings context;
	context.antialiasingLevel = 16;

	m_pWindow = new sf::RenderWindow(sf::VideoMode(1280, 720), "Pac-Man", sf::Style::Resize, context);
    
	//m_pWindow->setVerticalSyncEnabled(true);
	//m_pWindow->setFramerateLimit(60);
	m_pWindow->setKeyRepeatEnabled(false);
	m_pWindow->setActive(true);

	FPSTime = sf::Time::Zero;
	frameCount = 0;
	lastFrameCount = 0;
}

//-------------------------------------------------------------------------------------------
WindowManager::~WindowManager()
{
}

//-------------------------------------------------------------------------------------------
void WindowManager::Setup()
{
}

//-------------------------------------------------------------------------------------------
sf::RenderWindow* WindowManager::GetWindow()
{
	return m_pWindow;
}

//-------------------------------------------------------------------------------------------
float WindowManager::GetWidth()
{
	return m_pWindow->getSize().x;
}

//-------------------------------------------------------------------------------------------
float WindowManager::GetHeight()
{
	return m_pWindow->getSize().y;
}

//-------------------------------------------------------------------------------------------
void WindowManager::PreTick()
{
	m_pWindow->clear();
}

//-------------------------------------------------------------------------------------------
void WindowManager::DrawDebug(float DeltaTime)
{
	// Do FPS Counter
	FPSTime += sf::seconds(DeltaTime);
	frameCount++;
	if (FPSTime.asSeconds() >= 1)
	{
		FPSTime = sf::Time::Zero;
		lastFrameCount = frameCount;
		frameCount = 0;
	}

	//Debug Text
	sf::Text text_TickTime("Time: " + std::to_string((long double)DeltaTime));
	text_TickTime.setScale(0.5, 0.5);
	text_TickTime.setPosition(GetWidth() - 128, 0);
	m_pWindow->draw(text_TickTime);

	sf::Text text_FrameRate("FPS: " + std::to_string((long double)lastFrameCount));
	text_FrameRate.setScale(0.5, 0.5);
	text_FrameRate.setPosition(GetWidth() - 128, 20);
	m_pWindow->draw(text_FrameRate);
}

//-------------------------------------------------------------------------------------------
void WindowManager::Tick(float DeltaTime)
{
	// The window must be polled, or it will lock.
	sf::Event event;
	m_pWindow->pollEvent(event);

	// PAss events to the Game Service.
	sf::Event::KeyEvent* pKeyEvent = &event.key;

	if (event.type == sf::Event::KeyPressed)
	{
		GameService::GetInstance()->OnInput(pKeyEvent, true);
	}
	else if(event.type == sf::Event::KeyReleased)
	{
		GameService::GetInstance()->OnInput(pKeyEvent, false);
	}

	DrawDebug(DeltaTime);
}

//-------------------------------------------------------------------------------------------
void WindowManager::PostTick()
{
	m_pWindow->display();
}
//-------------------------------------------------------------------------------------------